<?php
/**
 * Created by PhpStorm.
 * User: aiChenK
 * Date: 2019-01-10
 * Time: 16:08
 */
namespace Services\Log;

use Helpers\Main;
use Helpers\Tool;
use Services\Log\Models\LoginLog;

class Log
{

    /**
     * 获取后台操作日志列表
     *
     * @param $params
     * @return array
     *
     * create by ck 20190110
     */
    public static function getCommitLog($params = [])
    {
        $conditions = [];
        if (Tool::getValue($params, 'submodule')) {
            $conditions[] = "sSubmodule like '%{$params['submodule']}%'";
        }
        if (Tool::getValue($params, 'operate')) {
            $conditions[] = "sOperate like '%{$params['operate']}%'";
        }
        if (Tool::getValue($params, 'explain')) {
            $conditions[] = "sExplain like '%{$params['explain']}%'";
        }
        if (Tool::getValue($params, 'content')) {
            $conditions[] = "MATCH(mContent) AGAINST ('*{$params['content']}*' IN BOOLEAN MODE)";
        }
        if (Tool::getValue($params, 'startDate')) {
            $conditions[] = "tCreateDate >= '{$params['startDate']}'";
        }
        if (Tool::getValue($params, 'endDate')) {
            $conditions[] = "tCreateDate <= '{$params['endDate']}'";
        }
        if ($conditions) {
            $conditions = 'WHERE ' . implode(' AND ', $conditions);
        } else {
            $conditions = '';
        }
        $sql = "SELECT * FROM XHLog_Commit {$conditions} ORDER BY iCommitId DESC";
        $data = Main::runSqlPage($sql);
        Main::runUserName($data['rows'], ['iUserId' => 'sUserName']);
        Main::runUserLogonName($data['rows'], ['iUserId' => 'sLogonName']);
        //替换操作内容文字为 点击查看
        foreach ($data['rows'] as &$row) {
            $row['mContent'] = htmlspecialchars($row['mContent']);
            $row['mContent'] = '<a href="javascript:;" onclick="obj.funObj().detail('.$row['mContent'].');">点击查看</a>';
        }
        unset($row);
        return $data;
    }

    /**
     * 获取应用错误日志
     *
     * @param array $params
     * @return array
     *
     * create by ck 20190114
     */
    public static function getErrorLog($params = [])
    {
        $parameters['order'] = 'iAEId DESC';
        Main::runSearchVagueConditions(
            $parameters['conditions'],
            ['iAppCode', 'iErrorCode', 'iUserId', 'sPackName', 'sDeviceId', 'sRequestFun', 'sLogType', 'tCreateDate']
        );
        return Main::runModelPage('\\Services\\Log\\Models\\XhlogApperror', $parameters);
    }

    /**
     * 获取平台登陆日志
     *
     * @param array $params
     * @param int $page
     * @param int $size
     * @return array
     *
     * create by ck 20190116
     */
    public static function getPlatformLoginLog($params = [], $page = 1, $size = 50)
    {
        $conditions = [];
        if (Tool::getValue($params, 'startDate')) {
            $conditions[] = "lo_date >= '{$params['startDate']}'";
        }
        if (Tool::getValue($params, 'endDate')) {
            $params['endDate'] = Tool::getEndTime($params['endDate'], false);
            $conditions[] = "lo_date < '{$params['endDate']}'";
        }
        if (Tool::getValue($params, 'item') && Tool::getValue($params, 'vague')) {
            $conditions[] = "{$params['item']} = '{$params['vague']}'";
        }
        $parameters = [
            'conditions' => implode(' AND ', $conditions),
            'order'      => 'lo_id desc'
        ];
        $pageResult = LoginLog::runPage($parameters, $page, $size);
        Main::runUserName($pageResult['rows'], ['lo_us_id' => 'username']);
        return $pageResult;
    }
}
